﻿#include "precomp.h"
#include "jthread.h"
#include "jthread_p.h"

// - class JThread -

J_IMPLEMENT_OBSERVER(JThread)

/**
 * @brief JThread::currentThreadId
 * @return
 */
JConcurrent::HANDLE JThread::currentThreadId()
{
    return (JConcurrent::HANDLE)JThreadDelegate::currentThreadId();
}

/**
 * @brief JThread::currentThread
 * @return
 */
JThread JThread::currentThread()
{
    return JThread(JThreadDelegate::currentThread());
}

/**
 * @brief JThread::idealThreadCount
 * @return
 */
int JThread::idealThreadCount()
{
    return JThreadDelegate::idealThreadCount();
}

/**
 * @brief JThread::yieldCurrentThread
 */
void JThread::yieldCurrentThread()
{
    JThreadDelegate::yieldCurrentThread();
}

/**
 * @brief JThread::JThread
 */
JThread::JThread()
    : d_ptr(new JThreadPrivate(this))
{
    J_D(JThread);
    d->init();
}

/**
 * @brief JThread::JThread
 * @param other
 */
JThread::JThread(QThread *other)
    : d_ptr(new JThreadPrivate(this, other))
{
    J_D(JThread);
    d->init();
}

/**
 * @brief JThread::~JThread
 */
JThread::~JThread()
{
    delete d_ptr;
}

/**
 * @brief JThread::setPriority
 * @param priority
 */
void JThread::setPriority(JThread::Priority priority)
{
    J_D(JThread);
    d->thread->setPriority((QThread::Priority)priority);
}

/**
 * @brief JThread::priority
 * @return
 */
JThread::Priority JThread::priority() const
{
    J_D(const JThread);
    return (JThread::Priority)d->thread->priority();
}

/**
 * @brief JThread::isFinished
 * @return
 */
bool JThread::isFinished() const
{
    J_D(const JThread);
    return d->thread->isFinished();
}

/**
 * @brief JThread::isRunning
 * @return
 */
bool JThread::isRunning() const
{
    J_D(const JThread);
    return d->thread->isRunning();
}

/**
 * @brief JThread::setStackSize
 * @param stackSize
 */
void JThread::setStackSize(unsigned int stackSize)
{
    J_D(JThread);
    d->thread->setStackSize((uint)stackSize);
}

/**
 * @brief JThread::stackSize
 * @return
 */
unsigned int JThread::stackSize() const
{
    J_D(const JThread);
    return (unsigned int)d->thread->stackSize();
}

/**
 * @brief JThread::exit
 * @param retcode
 */
void JThread::exit(int retcode)
{
    J_D(JThread);
    d->thread->exit(retcode);
}

/**
 * @brief JThread::start
 * @param priority
 */
void JThread::start(JThread::Priority priority)
{
    J_D(JThread);
    d->thread->start((QThread::Priority)priority);
}

/**
 * @brief JThread::terminate
 */
void JThread::terminate()
{
    J_D(JThread);
    d->thread->terminate();
}

/**
 * @brief JThread::quit
 */
void JThread::quit()
{
    J_D(JThread);
    d->thread->quit();
}

/**
 * @brief JThread::suspend
 */
void JThread::suspend()
{
    J_D(JThread);
    d->suspend();
}

/**
 * @brief JThread::resume
 */
void JThread::resume()
{
    J_D(JThread);
    d->resume();
}

/**
 * @brief JThread::wait
 * @param time
 * @return
 */
bool JThread::wait(unsigned long time)
{
    J_D(JThread);
    return d->thread->wait(time);
}

/**
 * @brief JThread::sleep
 * @param secs
 */
void JThread::sleep(unsigned long secs)
{
    JThreadDelegate::sleep(secs);
}

/**
 * @brief JThread::msleep
 * @param msecs
 */
void JThread::msleep(unsigned long msecs)
{
    JThreadDelegate::msleep(msecs);
}

/**
 * @brief JThread::usleep
 * @param usecs
 */
void JThread::usleep(unsigned long usecs)
{
    JThreadDelegate::usleep(usecs);
}

/**
 * @brief JThread::run
 */
void JThread::run()
{

}

/**
 * @brief JThread::exec
 * @return
 */
int JThread::exec()
{
    J_D(JThread);
    return d->exec();
}

/**
 * @brief JThread::setTerminationEnabled
 * @param enabled
 */
void JThread::setTerminationEnabled(bool enabled)
{
    JThreadDelegate::setTerminationEnabled(enabled);
}

/**
 * @brief JThread::data
 * @return
 */
QThread *JThread::data() const
{
    J_D(const JThread);
    return d->thread;
}
